package Month09.day0927;

import java.util.Arrays;

public class Student implements Comparable{
    String name;
    int score;

    public Student(String name,int score) {
        this.name = name;
        this.score = score;
    }


    public int compareTo(Object o){
        Student s = (Student)o;
        if (this.score > s.score) {
            return -1;
        }else if (this.score < s.score) {
            return 1;
        }else{
        return 0;
    }
}

    public String toString(){
        String s = "[";
        s += this.name + ",";
        s += this.score + "]";
        return s;
    }

    public static void sort(Comparable[] arr){
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if(arr[i].compareTo(arr[i+1]) > 0){
                    Comparable tmp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = tmp;
                }
            }
        }
    }

    public static void main(String[] args) {
        Student[] arr = {
            new Student("张三",18),
            new Student("李四",17),
            new Student("王五",19),
            new Student("赵六",20),
        };
        //接下来使用sort方法对其进行排序
        Arrays.sort(arr);
        for(Student i:arr){
            System.out.println(i);
        }
    }
}
